<?php
namespace app\api\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use app\api\model\UserModel as User;
use app\api\model\SmsModel;
  
class Sms extends Base
{
    public $user;
    public $sms;
    public function __construct()
    {
        parent::__construct();
        header("Access-Control-Allow-Origin: *");
        $this->user = new User();
        $this->sms = new SmsModel();
    }
	//发送短信
    public function SendSms()
    {
        header("Content-Type:text/html;charset=utf-8");
       // $apikey = "4fe2afce18b6ed90de06d79d836a0b98";//修改为您的apikey(ey(https://www.yunpian.com)登录官)登录官网后获取
        $mobile = $_POST['tel'];
        /*if($_POST['type'] == 1){
            $user_info = Db::name("user")->where("u_phone",$mobile)->find();
            if(empty($user_info)){
                $this->error("没有该用户！",'','-1');
            }
        }*/
		$code =  rand(100000,999999);
        // 发送模板短信
        // 需要对value进行编码
        
      	/*$data = array('tpl_id' => '150445', 'tpl_value' => ('#code#'). '='.urlencode($code), 'key' => $apikey, 'mobile' => $mobile);
        //print_r ($data);
        $json_data = $this->tpl_send($data);
        $array = json_decode($json_data,true);*/
      
      	$CorpID = "QTTX009037";//$_POST['CorpID']; //用户名
		$Pwd = "lv112358";//$_POST['Pwd']; //密码
		$Mobile = $mobile;//$_POST['Mobile']; //手机号
		$Content = "您的验证码是".$code."。如非本人操作请勿泄露给他人。"; //内容
		$ContentS = rawurlencode(mb_convert_encoding($Content, "gb2312", "utf-8"));//短信内容做GB2312转码处理
		$url = "https://sdk2.028lk.com/sdk2/LinkWS.asmx/BatchSend2?CorpID=".$CorpID."&Pwd=".$Pwd."&Mobile=".$Mobile."&Content=".$ContentS."&Cell=&SendTime=";
		$result=file_get_contents($url);
		$re=simplexml_load_string($result);
      	dump($re);die;
        if($re[0] > 0){
            $request = Request::instance();
            $ip = $request->ip();
            $sql = Db::name("sms")
                ->insert([
                    'code' => $code,
                    'times' => 0,
                    'createtime' => time(),
                    'ip' => $ip,
                    'mobile' => $mobile
                ]);
            if($sql){
                $this->success("发送成功！",'','1');
            }else{
                $this->error("发送失败！",'','-1');
            }
        }else{
            $this->error("发送失败！",'','-1');
        }
    }
    public function tpl_send($data){
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL,
            'http://v.juhe.cn/sms/send');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result,$error);
        return $result;
    }
    public function checkErr($result,$error) {
        if($result === false)
        {
            echo 'Curl error: ' . $error;
        }
        else
        {
            //echo '操作完成没有任何错误';
        }
    }
}
?>